package com.hrbxlh.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hrbxlh.actionImpl.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：RoleAction.java
* Description：角色action类
* History：
* Date           Author               Desc
* 2017.1.9		  xzx                 建立
* -------------------------------------------------------
*/

@Controller
public class RoleAction {
	
	@Autowired
	@Qualifier("roleActionImpl")
	private RoleActionImpl roleActionImpl;

	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;

	/**
	 * @param response
	 * @return  跳转到添加角色页面 (没用到)
	 */
	@RequestMapping(value = "/skipAddRole", method = RequestMethod.POST)
	public void skipAddRole( HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(RoleAction.class.getName());
		try {
			//查询全部菜信息
			//List<Map<String, Object>> menuList = menuActionImpl.getMenuList(resultMap);
		    //返回值
			//resultMap.put("menuList", menuList);
			//添加角色页面名称
			//pageName = "pc/addRole";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转添加角色页面出现异常", e);
			//pageName = "public/pageerror";
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @param response
	 * @return  添加角色,参数：角色名称，以及角色具有的权限
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addRole", method = RequestMethod.POST)
	public void addRole(String jsonStr , HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(RoleAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//添加角色
			Map<String, Object> flagMap = roleActionImpl.addRole(paramMap);
			//返回参数
			resultMark = ((Boolean)flagMap.get("result") == true) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg",flagMap.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加角色出现异常addRole", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @param response
	 * @return  进修改角色页面,参数:角色id，
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveRole", method = RequestMethod.POST)
	public void skipSaveRole(String jsonStr , HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(RoleAction.class.getName());
		try {
			//获取页面参数值
			Map<String,Object> preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取该角色信息
			List<Map<String, Object>> roleMessageList = roleActionImpl.getRoleList(preParamMap);
			if(roleMessageList != null && roleMessageList.size() != 0){
				resultMap.put("role", roleMessageList.get(0));
			}
			//获取角色具有的菜单权限
			//List<Map<String, Object>> permissionList = roleActionImpl.getPermissionByRoleId((Integer)preParamMap.get("roleId"));
			//resultMap.put("permissionList", permissionList);
			
			//获取全部菜单信息
			//List<Map<String, Object>> menuList = menuActionImpl.getMenuList(resultMap);
			//resultMap.put("menuList", menuList);
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转修改角色页面出现异常skipSaveRole", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param response  
	 * @return  修改角色,传回角色id，角色名称，权限集合
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveRole", method = RequestMethod.POST)
	public void saveRole(String jsonStr , HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(RoleAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			/*模拟参数开始*/
			//Map<String, Object> mapUse = new HashMap<String, Object>();
			//mapUse.put("roleId", 2);
			//mapUse.put("roleName", "FFFF");
			//List<Integer> list = new ArrayList<Integer>();
			//list.add(4);
			//list.add(5);
			//list.add(6);
			//mapUse.put("permissionList", list);
			//jsonStr = mapper.writeValueAsString(mapUse);
			/*模拟参数结束*/
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //修改角色
			Map flagMap = roleActionImpl.saveRole(paramMap);
			resultMark = ((Boolean)flagMap.get("result") == true) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", flagMap.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("修改角色出现异常saveRole", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**删除角色
	 * @param response
	 * @param jsonStr 参数：角色id，List<String> 形式   jsonStr = ["1","2","3"...]
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delRoles", method = RequestMethod.POST)
	public void delRoles(String jsonStr , HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(RoleAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
            //删除角色操作
			Map<String, String> delResult = roleActionImpl.delRole(paramList);
			//返回成功失败
			resultMark = (delResult.get("result").equals("true")) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("errorMsg", delResult.get("errorMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除角色出现异常delRoles", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**角色列表页面
	 * @param jsonStr = pageNow,pageSize(一般是10)，页数从1开始，第一页pageNow传1  搜索条件roleName
	 * @return  传回全部角色信息。进页面，翻页，条件查询都是这个。
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getRoleList", method = RequestMethod.POST)
	public void getRoleList(String jsonStr, HttpServletResponse response)   {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(RoleAction.class.getName());
		try {
			//获取页面参数值
			Map<String, Object> preParamMap = mapper.readValue(jsonStr, returnMap.getClass());
			//获取角色列表信息
			Map<String, Object> resultAllMap = roleActionImpl.getRoleListByPage(preParamMap);
			//返回值
			resultMap.put("roleList", resultAllMap.get("roleList"));
			resultMap.put("totalPage", resultAllMap.get("totalPage"));
			resultMap.put("pageNow", resultAllMap.get("pageNow"));
			resultMap.put("pageSize", preParamMap.get("pageSize"));
			resultMap.put("roleName", preParamMap.get("roleName"));
			resultMap.put("roleId", preParamMap.get("roleId"));
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("角色列表页面出现异常getRoleList", e);
		} finally {
			try {
				courseActionImpl.responseStream(response, mapper.writeValueAsString(resultMap));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}
	}
}